from my_package import MyPackage as pkg
import time
from client import *
import pygame

Up = 273
Dowm = 274
Left = 276
Right = 275
#遥控器类

class Controller:
    def __init__(self):
        self.running = False
        self.index = 1
        self.package = pkg(1)
        self.CartIndex = 1
        self.VelScale = 60
        self.RequiredVelocity = [0, 0, 0]
        self.timeout = 2
        self.basicRotate = 0
        pygame.init()
        screen = pygame.display.set_mode((200,30))

    def connect(self):
        self.client = MyClient(2, 1, '139.9.227.196', 8083)
        self.client.login(f)
        self.running = True

    def disconnect(self):
        self.running = False
        self.client.close()

    def setVelocity(self, vx, vy, omega, base = 1):  #控制车的运动速度，每个值范围为(-1,1)
        self.RequiredVelocity = [vx, vy, omega + self.basicRotate]
        msg = self.package.put(1, 1, [int(vx * self.VelScale + 64), int(vy * self.VelScale + 64), int(omega * self.VelScale + 64), base])
        self.client.send(msg)

    def acceptInput(self):
        while True:
            time.sleep(0.02)
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    self.disconnect()
                    exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == Up:
                        c.setVelocity(0, 1, 0)
                    elif event.key == Dowm:
                        c.setVelocity(0, -1, 0)
                    elif event.key == Right:
                        c.setVelocity(0, 0, -1)
                    elif event.key == Left:
                        c.setVelocity(0, 0, 1)
                if event.type == pygame.KEYUP:
                    c.setVelocity(0, 0, 0)

if __name__ == "__main__":
    c = Controller()
    c.connect()
    c.acceptInput()
    while True:
        c.setVelocity(0,1,0)
        time.sleep(1)
        c.setVelocity(0,0,0)
        time.sleep(1)


        
